package com.neshai.webapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neshai.webapp.entity.AccountBill;
import com.neshai.webapp.entity.EthereumTransaction;
import com.neshai.webapp.entity.Invoices;
import com.neshai.webapp.mapper.AccountBillMapper;
import com.neshai.webapp.mapper.EthereumTransactionMapper;
import com.neshai.webapp.mapper.InvoicesMapper;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping({"/api"})
@RequiredArgsConstructor
public class BillController {

    private final EthereumTransactionMapper ethereumTransactionMapper;
    private final InvoicesMapper invoicesMapper;
    private final AccountBillMapper accountBillMapper;

    @GetMapping("/bill/wallet")
    public List<EthereumTransaction> getWeb3WalletBill(HttpServletRequest request) {
        Object idObject = request.getAttribute("id");
        if (idObject instanceof String) {
            List<EthereumTransaction> list = ethereumTransactionMapper.selectList(
                    new QueryWrapper<EthereumTransaction>().eq("user_id", Integer.valueOf((String) idObject)).select("hash", "is_success", "sender_address", "value", "created_at")
            );
            return list;
        } else {
            return null;
        }
    }

    @GetMapping("/bill/stripe")
    public List<Invoices> getStripeInvoice(HttpServletRequest request) {
        Object claimsObject = request.getAttribute("claims");
        if (claimsObject instanceof Claims) {
            List<Invoices> list = invoicesMapper.selectList(
                    new QueryWrapper<Invoices>().eq("customer_email", ((Claims) claimsObject).get("email")).select("invoice", "status", "customer_email", "currency", "hosted_invoice_url", "completed_at", "amount_paid")
            );
            return list;
        } else {
            return null;
        }
    }

    @GetMapping("/bill/account")
    public List<AccountBill> getAccountInvoice(HttpServletRequest request) {
        Object idObject = request.getAttribute("id");
        if (idObject instanceof String) {
            List<AccountBill> list = accountBillMapper.selectList(
                    new QueryWrapper<AccountBill>().eq("user_id", Integer.valueOf((String) idObject)).select("created_at", "paid_type", "amount_paid")
            );
            return list;
        } else {
            return null;
        }
    }
}
